import Cookies from "js-cookie";
import axios from "axios";
import router from '../router';

const uri = 'http://localhost:9700';

/* get请求 */
let get = async (url) => {
    let token = Cookies.get('token');
    let headers = {
        headers: {
            'token': token
        }
    };
    let res = await axios.get(uri + url, headers).catch(function (error) {
        console.log("error.response = ", error.response);
        // 如果401,跳转到登录页面
        if (error.response.status == 401) {
            router.push('/login').catch(err => err);
        }
    });
    return res.data;
};

/* post请求 */
let post = async (url, params) => {
    let token = Cookies.get('token');
    let headers = {
        headers: {
            'token': token
        }
    };
    let res = await axios.post(uri + url, params, headers).catch(function (error) {
        console.log("error.response = ", error.response);
        // 如果401,跳转到登录页面
        if (error.response.status == 401) {
            router.push('/login').catch(err => err);
        }
    });
    return res.data;
};

/* put请求 */
let put = async (url, params) => {
    let token = Cookies.get('token');
    let headers = {
        headers: {
            'token': token
        }
    };
    let res = await axios.put(uri + url, params, headers).catch(function (error) {
        console.log("error.response = ", error.response);
        // 如果401,跳转到登录页面
        if (error.response.status == 401) {
            router.push('/login').catch(err => err);
        }
    });
    return res.data;
};

/* delete请求 */
let del = async (url, params) => {
    let token = Cookies.get('token');
    let res = await axios({
        method: 'delete',
        url: uri + url,
        headers: {
            'token': token
        }
    }).catch(function (error) {
        console.log("error.response = ", error.response);
        // 如果401,跳转到登录页面
        if (error.response.status == 401) {
            router.push('/login').catch(err => err);
        }
    });;
    return res.data;
};

/* 文件下载post请求 */
let postDownload = async (url, params) => {
    let token = Cookies.get('token');
    let res = await axios({
        method: 'post',
        url: uri + url,
        data: params,
        headers: {
            'token': token,
        },
        responseType: 'blob',
    }).catch(function (error) {
        console.log("error.response = ", error.response);
        // 如果401,跳转到登录页面
        if (error.response.status == 401) {
            router.push('/login').catch(err => err);
        }
    });
    if (res.headers['content-type'].includes('APPLICATION/OCTET-STREAM')) {
        /* 截取头部获取文件名 utf8转中文 */
        let str = res.headers['content-disposition']
        let fileName = decodeURI(str.split('=')[1]);
        /* 下载文件 */
        downloadFile(res, fileName)
    } else if (res.headers['content-type'].includes('application/json')) {
        //let enc = new TextDecoder('utf-8')
        //this.$message.error(enc.decode(new Uint8Array(res.data)))
    }
};

/* 导出文件 bold文件流方式导出 */
function downloadFile(res, fileName) {
    let url = window.URL.createObjectURL(new Blob([res.data]));
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
}

let http = {
    uri: uri,
    get: get,
    post: post,
    put: put,
    del: del,
    postDownload: postDownload
};

export default http;
